package com.chenbei.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.system.domain.SysPost;
import com.chenbei.system.mapper.SysPostMapper;
import com.chenbei.system.service.ISysPostService;
import com.chenbei.system.vo.req.SysPostReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPostService extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Override
    public void add(SysPost sysPost) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPost::getName, sysPost.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("岗位名称已存在");
        }
        if (sysPost.getSort() == null) {
            sysPost.setSort(0);
        }
        save(sysPost);
    }

    @Override
    public void update(SysPost sysPost) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(SysPost::getId, sysPost.getId())
                .eq(SysPost::getName, sysPost.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("岗位名称已存在");
        }
        updateById(sysPost);
    }

    @Override
    public void delete(long id) {
        removeById(id);
    }

    @Override
    public Page<SysPost> pageList(SysPostReq sysPostReq) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(SysPost::getSort);
        if (StringUtils.isNoneBlank(sysPostReq.getName())) {
            lambdaQueryWrapper.like(SysPost::getName, sysPostReq.getName());
        }
        if (sysPostReq.getStatus() != null && sysPostReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysPost::getStatus, sysPostReq.getStatus());
        }
        if (StrUtil.isNotBlank(sysPostReq.getCode())) {
            lambdaQueryWrapper.like(SysPost::getCode, sysPostReq.getCode());
        }
        Page<SysPost> pageConfig = new Page<>(sysPostReq.getPageNum(), sysPostReq.getPageSize());
        return page(pageConfig, lambdaQueryWrapper);
    }

    @Override
    public List<SysPost> list(SysPostReq sysPostReq) {
        LambdaQueryWrapper<SysPost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(SysPost::getSort);
        if (StringUtils.isNoneBlank(sysPostReq.getName())) {
            lambdaQueryWrapper.like(SysPost::getName, sysPostReq.getName());
        }
        if (sysPostReq.getStatus() != null && sysPostReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysPost::getStatus, sysPostReq.getStatus());
        }
        if (StrUtil.isNotBlank(sysPostReq.getCode())) {
            lambdaQueryWrapper.like(SysPost::getCode, sysPostReq.getCode());
        }

        return list(lambdaQueryWrapper);
    }


    @Override
    public SysPost getBySysUserId(Long sysUserId) {
        return baseMapper.getBySysUserId(sysUserId);
    }
}
